package com.prdiot.poetry.dao;

import com.prdiot.poetry.domains.Chengyu;
import com.prdiot.poetry.domains.Mingju;
import com.prdiot.poetry.domains.Poems;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface ChengyuRepository extends JpaRepository<Chengyu, Integer> {

    @Query("select c from Chengyu c where c.chengyu like %?1%")
    Page<Chengyu> searchByKey(@Param("key") String key, Pageable pageable);


    @Query(value = "SELECT * FROM chengyu WHERE id>=(SELECT FLOOR(RAND() * (SELECT MAX(id) FROM chengyu)))  ORDER BY id LIMIT 60", nativeQuery=true)
    List<Chengyu> getRandChengyu();

    @Query("select c from Chengyu c ")
    Page<Chengyu> getChengyuList(Pageable pageable);

    Optional<Chengyu> findByCode(String code);

}
